<template>
  <div>
    <el-form :inline="true" :model="queryParams" class="demo-form-inline">
      <el-form-item label="公司名称">
        <el-input v-model="queryParams.coName" placeholder="公司名称"></el-input>
      </el-form-item>
      <el-form-item label="公司代码">
        <el-input v-model="queryParams.coCode" placeholder="公司代码"></el-input>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-plus" size="mini" @click="query">查询
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-plus" size="mini" @click="reSet">清除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" @click="addHandler()" size="mini">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" @click="deleteHandler()" size="mini">删除</el-button>
      </el-col>
    </el-row>
    <br>
    <el-table :data="tableData" stripe v-loading="loading" style="width: 100%"
              @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column prop="coId" label="公司主键"></el-table-column>
      <el-table-column prop="coName" label="公司名称"></el-table-column>
      <el-table-column prop="coCode" label="公司代码"></el-table-column>
      <el-table-column class="caoZuo" label="操作" width="200px">
        <template slot-scope="scope">
          <el-button type="warning" size="mini" @click="editHandler(scope.row.coId)">修改</el-button>
          <el-button type="danger" size="mini" @click="deleteHandler(scope.row.coId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryParams.pageNum"
        :page-sizes="[5, 10, 50, 100]"
        :page-size="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

    <el-dialog
        :title="title"
        :visible.sync="dialogVisible"
        width="30%"
        append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="公司名称" prop="coName">
          <el-input placeholder="请输入公司名称" v-model="form.coName" ></el-input>
        </el-form-item>
        <el-form-item label="公司代码" prop="coCode">
          <el-input placeholder="请输入公司代码" v-model="form.coCode" ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit()">保存</el-button>
          <el-button @click="dialogVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {getPage,editPage,editSave,addSave,deleteIds} from "@/api/demo/Company"

export default {
  title: '',
  //用于删除的id集合
  ids: [],
  data() {
    return {
      total:0,
      loading:false,
      title: '',
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        coName:'',
        coCode:'',
      },
      form: {
        coId: null,
        coName: null,
        coCode: null
      },
      rules: {
        coName: [
          {required: true, message: '请输入公司名称', trigger: 'blur'},
          {min: 3, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur'}
        ],
        coCode: [
          {required: true, message: '请输入公司代号', trigger: 'blur'}
        ],
      },
      dialogVisible: false,
      formLabelWidth: '120px',
      tableData: [],
    }
  },
  created() {
    // vue实例创建完成 执行的钩子函数
    // 请求服务器
    this.getList()
  },
  methods: {
    getList(){
      this.loading=true
      // 写请求
      getPage(this.queryParams).then(resp => {
        // console.log(resp.data)
        this.tableData=resp.rows
        this.loading=false
        this.total = resp.total
      })

    },/**
     * 清空表单
     */
    resetForm(){
      this.form = {

      }
    },
    /**
     * 打开添加 对话框
     */
    addHandler() {
      this.resetForm()
      this.title = '添加信息'
      this.dialogVisible = true
    },
    /**
     * 打开修改 对话框
     */
    editHandler(id) {
      console.log(id)
      editPage(id).then(resp => {
        this.form = resp
        this.title = '修改信息'
        this.dialogVisible = true
      })
    },
    /**
     * 添加保存处理
     */
    onSubmit() {
      // 通过选择器找到form表单进行处理
      this.$refs['form'].validate((valid) => {
        if (valid) {
          //通过了校验
          if (this.form.coId) {
            // 修改
            // axios处理传值进行添加处理
            editSave(this.form).then(resp => {
              //判断成功还是失败,给予明确提示
              if (resp.code == 200) {
                //弹出成功消息
                this.$message({
                  message: '保存成功',
                  type: 'success'
                });
                // 关闭显示窗
                this.dialogVisible = false
                // 刷新页面
                this.getList()
              } else {
                // 弹出失败消息
                this.$message.error('保存失败');
              }
            })
          }else{
            // 添加
            // axios处理传值进行添加处理
            addSave(this.form).then(resp => {
              //判断成功还是失败,给予明确提示
              if (resp.code == 200) {
                //弹出成功消息
                this.$message({
                  message: '保存成功',
                  type: 'success'
                });
                // 关闭显示窗
                this.dialogVisible = false
                // 刷新页面
                this.getList()
              } else {
                // 弹出失败消息
                this.$message.error('保存失败');
              }
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }


      });
    },
    /**
     * 删除处理
     * @param id
     */
    deleteHandler(id) {
      // console.log(id)
      //判断传入的是单条操作还是多选操作
      let idss = id || this.ids.join(',')
      // axios处理传值进行删除处理
      deleteIds(idss).then(resp => {
        //判断成功还是失败
        if (resp.code == 200) {
          //弹出成功消息
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          // 刷新页面
          this.getList()
        } else {
          // 弹出失败消息
          this.$message.error('删除失败');
        }
      }).catch(err => {
        // 发生异常时,弹出异常消息
        this.$message.error('删除异常');
      })
    },
    /**
     *     多选 选中处理
     */
    handleSelectionChange(val) {
      // console.log(val)
      this.ids = val.map(item => item.coId)
      console.log(this.ids)
    },/**
     * 查询处理
     */
    query() {
      this.getList()
    },
    /**
     * 重置处理
     */
    reSet() {
      this.queryParams.coName=''
      this.queryParams.coCode=''
    },
    handleSizeChange(val){
      // console.log(val)
      this.queryParams.pageSize = val
      this.getList()
    },
    handleCurrentChange(val){
      // console.log(val)
      this.queryParams.pageNum = val
      this.getList()
    },
    handleClick() {
      alert('button click');
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {
          });
    },
    logout() {
      this.$confirm('是否要退出登录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$router.push({path: "/"});
        this.$message({
          type: 'success',
          message: '退出成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消退出'
        });
      });

    }

  }
}
</script>

<style scoped lang="scss">
.el-form {
  margin: 20px auto;
  width: 90%;
  text-align: left;
}
</style>
